import service from '@/service.js'
import sdkAction from "mcbluetoothsdk";
 
var app = getApp();
var out_time =0;
var outClient =0;
var connStatus =0;
const getBLEDeviceCharacteristics = (dev,success,fail)=>{
	//#ifdef MP-WEIXIN
	let _globalData = getApp()
	let _module = uni
	//#endif
	
	//#ifdef APP-PLUS
	let _globalData = getApp().globalData
	let _module = wx
	//#endif
	
	 
 
	sdkAction.initObjDefault(_globalData, _module, {
		'_appId': app.globalData.appId,
		'_wxSdkKey': app.globalData.wxSdkKey
	},function(res) {
		console.log(333, res)
		console.log('当前deviceId----',dev.deviceId)
		if(res){
			app.globalData.sdkAccessToken = res.data.sdkAccessToken 
			sdkAction.doInitMulti(dev.deviceId, function() {
				console.log('sinocare初始化链接成功')
			
				//链接设备  设备类型 （唯一）  mac地址   回调
				console.log('官方设备类型----',dev.deviceType)
				sdkAction.startMultiConnectDevice(dev.deviceType, dev.deviceId,
					function(res) {
						console.log('sinocare链接设备成功:返回测量结果')
						//测量结果从这返回
						console.log('sinocare---res', res)
					 
						if(res.durDeviceInfo){
							let devInfo = res.durDeviceInfo
							let GLU =0
							
							//血糖
							 GLU = res.GLU 
							 success({
							 	status: 1,
							 	code:'onBLECharacteristicValueChange',
							 	data:{deviceId:dev.deviceId,mol:GLU,unit:0,factory:'sinocare'},
							 	message:'设备测量数据'
							})  
						}
			
					})
			}) 
			 
		}
	
	})  
	
	//监听蓝牙设备连接状态
	sdkAction.dispatchConnectState(function(res) {
		console.log('sdk蓝牙设备状态-----',res)
		  
		if(res){
			if(!res[0].connected){
				console.log(33333222)
				fail({
					status:0,
					code:10,
					data:{connected:false}, 
					message:'连接蓝牙设备失败1'
				}) 
				
				sdkAction.closeBlueTooth(function(resutl) {
					console.log('关闭链接----',resutl)
				})
				  
				return 
				 
			}else{
				console.log('----2222')
			} 
		}
	})
	 
}
 
 
export default {
	getBLEDeviceCharacteristics
}